package com.zzh.takeoutapp.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.activity.AddressActivity;
import com.zzh.takeoutapp.activity.CollectOrderActivity;
import com.zzh.takeoutapp.activity.CommentActivity;
import com.zzh.takeoutapp.activity.HeadPortraitActivity;
import com.zzh.takeoutapp.activity.HelpActivity;
import com.zzh.takeoutapp.activity.LoginActivity;
import com.zzh.takeoutapp.activity.SettingActivity;
import com.zzh.takeoutapp.base.BaseFragment;
import com.zzh.takeoutapp.config.Constants;
import com.zzh.takeoutapp.entity.OrderListBean;
import com.zzh.takeoutapp.event.NetMsgEvent;
import com.zzh.takeoutapp.service.EventbusHelper;
import com.zzh.takeoutapp.service.ServiceUrl;
import com.zzh.takeoutapp.utils.GsonResolve;
import com.zzh.takeoutapp.utils.ImageManager;
import com.zzh.takeoutapp.utils.SPUtils;
import com.zzh.takeoutapp.utils.UtilTool;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/11.
 */

public class MineFragment extends BaseFragment {
    @Bind(R.id.iv_setting)
    ImageView iv_setting;
    @Bind(R.id.me_headportrait)
    ImageView me_headportrait;
    @Bind(R.id.tv_nickname)
    TextView tv_nickname;
    @Bind(R.id.tv_mobile)
    TextView tv_mobile;
    @Bind(R.id.mine_collect)
    LinearLayout mCollect;
    @Bind(R.id.mine_comment)
    LinearLayout mineComment;
    @Bind(R.id.mine_footprint)
    LinearLayout mineFootprint;
    @Bind(R.id.mine_Address)
    LinearLayout mineAddress;
    @Bind(R.id.mine_wallet)
    LinearLayout mine_wallet;
    @Bind(R.id.mine_voucher)
    LinearLayout mine_voucher;
    @Bind(R.id.mine_packet)
    LinearLayout mine_packet;
    @Bind(R.id.mine_help)
    LinearLayout mine_help;
    @Bind(R.id.mine_head_portrait)
    RelativeLayout mineHeadPortrait;
    private final String MINE_FRAGMENT = "MineFragment";
    private String face;
    private String mobile;
    private String nickname;
    private String uid;

    @Override
    public void getsavedInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container) {
        View mView = inflater.inflate(R.layout.fragment_mine, null);
        ButterKnife.bind(this, mView);
        tv_nickname.setText("请点击登录");
        return mView;
    }

    @Override
    public void initComponent(View view) {
        ServiceUrl.UserInfoService(getActivity(), MINE_FRAGMENT);
    }

    @Override
    public void setListener() {

    }

    @Override
    public void onAfterInit() {

    }

    @Override
    public void onStart() {
        super.onStart();
        EventbusHelper.register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventbusHelper.unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(NetMsgEvent event) {
        String _msgType = (String) event.getMsgType();
        JSONObject jsonObject = event.getMsgJSONObj();
        switch (_msgType) {
            case MINE_FRAGMENT:
                if (jsonObject != null) {
                    if (jsonObject.optString("error").equals("0")) {
                        JSONObject jsonObject1 = jsonObject.optJSONObject("data");
                        uid = jsonObject1.optString("uid");
                        mobile = jsonObject1.optString("mobile");
                        nickname = jsonObject1.optString("nickname");
                        face = jsonObject1.optString("face");
                        ImageManager.loadUrlImage(getActivity(), jsonObject1.optString("face"), me_headportrait);
                        tv_nickname.setText(jsonObject1.optString("nickname"));
                        tv_mobile.setText("手机号:" + jsonObject1.optString("mobile"));
                    }
                }
                break;
        }
    }


    @OnClick({R.id.mine_collect, R.id.mine_footprint, R.id.mine_comment, R.id.mine_Address
            , R.id.mine_packet, R.id.mine_voucher, R.id.mine_wallet, R.id.mine_help,
            R.id.mine_head_portrait, R.id.iv_setting})
    @Override
    public void onClick(View view) {
        String token = SPUtils.getString(getActivity(), Constants.TOKEN, "");
        Intent intent = new Intent();
        if (token == null || token.equals("")) {
            intent.setClass(getActivity(), LoginActivity.class);
            intent.setType("MineFragment");
            start_Activity(intent);
        } else {
            switch (view.getId()) {
                case R.id.mine_collect:
                    intent.setClass(getActivity(), CollectOrderActivity.class);
                    intent.putExtra("mine_collect", "mine_collect");
                    start_Activity(intent);
                    break;
                case R.id.mine_footprint:
                    intent.setClass(getActivity(), CollectOrderActivity.class);
                    intent.putExtra("mine_collect", "mine_footprint");
                    start_Activity(intent);
                    break;
                case R.id.mine_comment:
                    intent.putExtra("face", face);
                    intent.putExtra("nickname", nickname);
                    intent.setClass(getActivity(), CommentActivity.class);
                    start_Activity(intent);
                    break;
                case R.id.mine_Address:
                    intent.setClass(getActivity(), AddressActivity.class);
                    getActivity().startActivity(intent);
                    break;
                case R.id.mine_packet:
                    UtilTool.toast(getActivity(), "功能开发中~");
                    break;
                case R.id.mine_voucher:
                    UtilTool.toast(getActivity(), "功能开发中~");
                    break;
                case R.id.mine_wallet:
                    intent.setClass(getActivity(), LoginActivity.class);
                    start_Activity(intent);
                    break;
                case R.id.mine_help:
                    intent.setClass(getActivity(), HelpActivity.class);
                    start_Activity(intent);
                    break;
                case R.id.mine_head_portrait:
                    intent.putExtra("face", face);
                    intent.putExtra("mobile", mobile);
                    intent.putExtra("nickname", nickname);
                    intent.putExtra("uid", uid);
                    intent.setClass(getActivity(), HeadPortraitActivity.class);
                    start_Activity(intent);
                    break;
                case R.id.iv_setting:
                    intent.setClass(getActivity(), SettingActivity.class);
                    start_Activity(intent);
                    break;
            }
        }
    }
}
